package cn.homework.dao.impl;

import cn.homework.dao.LoginDao;
import cn.homework.domain.Admin;
import cn.homework.domain.Customers;
import cn.homework.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author ACT
 */
public class LoginDaoImpl implements LoginDao {
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public Customers findCustomerByNameAndPassword(Customers customers) {
        String sql = "select * from customers where name = ? and password = ?";
        try {
            Customers customer = template.queryForObject(sql, new BeanPropertyRowMapper<Customers>(Customers.class),
                    customers.getName(), customers.getPassword());
            return customer;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Admin findAdminByNameAndPassword(Admin admin) {
        String sql = "select * from admin where name = ? and password = ?";
        try {
            Admin user = template.queryForObject(sql, new BeanPropertyRowMapper<Admin>(Admin.class),
                    admin.getName(), admin.getPassword());
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Customers findCustomerByCondition(Customers customers) {
        String sql = "select * from customers where name = ? and question = ? and answer = ? and email = ?";
        try {
            Customers customer = template.queryForObject(sql, new BeanPropertyRowMapper<>(Customers.class),
                    customers.getName(), customers.getQuestion(), customers.getAnswer(), customers.getEmail());
            return customer;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Admin findAdminByCondition(Admin admin) {
        String sql = "select * from admin where name = ? and email = ?";
        try {
            Admin user = template.queryForObject(sql, new BeanPropertyRowMapper<>(Admin.class),
                    admin.getName(), admin.getEmail());
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
